package ar.com.angel.thirds.components.zoom
{
    import flash.events.Event;
    import flash.events.MouseEvent;

    import mx.containers.Canvas;
    import mx.controls.Image;
    import mx.controls.Alert;
    import mx.core.UIComponent;

    public class ZoomFx
    {
        private var _stage:UIComponent;
        private var _fotoZoom:UIComponent;
        private var _panelZoom:UIComponent;
        private var _distX:Number;
        private var _distY:Number;
        private var _porcentajeX:Number;
        private var _porcentajeY:Number;

        //----------------------------------------------------
        public function ZoomFx(stage:UIComponent, fotoZoom:UIComponent, panelZoom:UIComponent)
        {
            _stage=stage;
            _fotoZoom=fotoZoom;
            _panelZoom=panelZoom;

            iniZoomDrag();
        }

        private function iniZoomDrag():void
        {
            var drag:Canvas=_panelZoom.getChildByName("zoomDrag") as Canvas;
            drag.addEventListener(MouseEvent.MOUSE_DOWN, onStartDrag);
            drag.addEventListener(MouseEvent.MOUSE_UP, onStopDrag);
            drag.addEventListener(MouseEvent.MOUSE_OUT, onStopDrag);
        }

        private function onStartDrag(e:Event):void
        {
            var canvas:Canvas=e.target as Canvas;
            canvas.startDrag();
            _stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseDragMove);
        }

        private function onStopDrag(e:Event):void
        {
            var canvas:Canvas=e.target as Canvas;
            canvas.stopDrag();
            _stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseDragMove);
        }

        private function onMouseDragMove(e:Event):void
        {
            calculaDist();
            mueveVisorZona();
            controlaPosiciones();
        }

        //-------------------------------------------------------------
        private function calculaDist():void
        {
            var drag:Canvas=_panelZoom.getChildByName("zoomDrag") as Canvas;
            var foto:Image=_fotoZoom.getChildByName("foto") as Image;

            _porcentajeX=100 / (foto.width / _panelZoom.width);
            _porcentajeY=100 / (foto.height / _panelZoom.height);

            drag.width=_fotoZoom.width * _porcentajeX / 100;
            drag.height=_fotoZoom.height * _porcentajeY / 100;

            _distX=drag.x * 100 / _porcentajeX;
            _distY=drag.y * 100 / _porcentajeY;

            _distX=(_distX <0) ? 0 : _distX;
            _distY=(_distY <0) ? 0 : _distY;
        }

        private function mueveVisorZona():void
        {
            var drag:Canvas=_panelZoom.getChildByName("zoomDrag") as Canvas;
            var foto:Image=_fotoZoom.getChildByName("foto") as Image;
            foto.x=-_distX;
            foto.y=-_distY;
        }

        private function controlaPosiciones():void
        {
            var drag:Canvas=_panelZoom.getChildByName("zoomDrag") as Canvas;
            var foto:Image=_fotoZoom.getChildByName("foto") as Image;

            if (drag.x <0)
            {
                drag.x=0;
            }
            else if (drag.x> _panelZoom.width - drag.width)
            {
                drag.x=_panelZoom.width - drag.width;
            }
            if (drag.y <_panelZoom.y)
            {
                drag.y=_panelZoom.y;
            }
            else if (drag.y> _panelZoom.y + _panelZoom.height - drag.height)
            {
                drag.y=_panelZoom.y + _panelZoom.height - drag.height;
            }

            if (foto.x> 0)
            {
                foto.x=0;
            }
            else if (foto.x <_fotoZoom.width - foto.width)
            {
                foto.x=_fotoZoom.width - foto.width;
            }
            if (foto.y> 0)
            {
                foto.y=0;
            }
            else if (foto.y <_fotoZoom.height - foto.height)
            {
                foto.y=_fotoZoom.height - foto.height;
            }
        }

    }
}